(function($) {
	$.fn.search = function(options) {
		defaults = {
			type: null,
			mainCell: null, //最外层的classname或者idname
			parentCell: null, //父元素的classname或者idname
			data: null, // 数组
			result: null, //获得的数据
			font: '',
			anchorShow: '',
			overdueAllData: '',
			serchInput: '',
			divHeight: null,
			nextCell: '',
			callback: function() {} //回调函数;
		};
		return this.each(function() {
			var opts = $.extend(defaults, options),
				type = opts.type,
				mainCell = opts.mainCell,
				parentCell = opts.parentCell,
				result = opts.result,
				overdueAllData = opts.overdueAllData,
				nextCell = opts.nextCell,
				anchorShow = opts.anchorShow,
				data = opts.data,
				callbank = opts.callback,
				divHeight = opts.divHeight,
				serchInput = opts.serchInput,
				font = opts.font,
				_this = $(this),
				anchor_list = [];

			$(overdueAllData).append('<p>' + result.all_num + '位' + font + '/' + font + '总金额' + result.all_amount + '元</p>');

			//创建dom
			var establish = function(datas, types) {
				var mark = 1;

				for(var i = 0; i < $('.overdue-tit').length; i++) {
					if(document.getElementsByClassName('overdue-tit')[i].innerText == types) {
						mark = 0;
					}
				}
				if(mark) {
					var overdue_tit = document.createElement('li');
					overdue_tit.className = "overdue-tit";
					overdue_tit.setAttribute('data-capital', types);
					overdue_tit.innerText = datas.initials;
					_this.append(overdue_tit);
				}

				var overdue_list = document.createElement('li');
				overdue_list.className = "overdue-list";
				overdue_list.setAttribute('data-name', datas.name);
				overdue_list.setAttribute('data-phone', datas.phone);
				var str = '';
				str += '<div class="flex-1"><a href="/admin/mobile/mobile_cuishou/detail?cid='+datas.id+'"><img class="head-portrait" src="' + datas.head_portrait + '" /></a></div>';
				str += '<div class="flex-2"><a href="/admin/mobile/mobile_cuishou/detail?cid='+datas.id+'"><p class="overdue-name">' + datas.name + '</p><p class="overdue-phone"><i class="phone-icon"></i>' + datas.phone + '</p></a></div>';
				str += '<div class="flex-1"><p class="voerdue-money">' + datas.amount + '</p></div>';
				str += '<div class="flex-1"><p class="voerdue-state">逾期<em>' + Math.floor(parseInt(datas.overdue_days)) + '</em>天</p></div>';
				overdue_list.innerHTML = str;
				_this.append(overdue_list);
			}

			function searchof(copy) {
				$.each(copy, function(i) {
					switch($(this)[0].initials) {
						case 'A':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}

							break;
						case 'B':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'C':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'D':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'E':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'F':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'G':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'H':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'I':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'J':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'K':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'L':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'M':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'N':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'O':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'P':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'Q':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'R':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'S':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'T':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'U':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'V':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'W':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'X':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'Y':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						case 'Z':
							establish($(this)[0], $(this)[0].initials);
							if($.inArray($(this)[0].initials, anchor_list) < 0) {
								anchor_list.push($(this)[0].initials)
							}
							break;
						default:
							alert('数据异常！！！，检查json数据');
					}
				});
			}
			//页面加载时候，调用searchof用来生成dom
			switch(type) {
				case 0:
					//生成anchor-list 列表
					searchof(data);
					var str = '';
					$.each(anchor_list, function(index) {
						str += '<a href="javascript:void(0)">' + anchor_list[index] + '</a>'
					});
					$(nextCell).append(str).css('margin-top', -(anchor_list.length * 20 / 2)).show();
					//快捷定位事件
					var offset_top = 0;
					$(nextCell).find('a').click(function() {
						var val = $(this).text();
						$(anchorShow).show().text(val);
						setTimeout(function() {
							$(anchorShow).hide().text('')
						}, 400);
						$('.overdue-tit').each(function(i) {
							if($(this).text() == val) {
								if($(mainCell).scrollTop() <= $(parentCell).height() - $(mainCell).height()) {
									offset_top += parseInt($(this).offset().top) - divHeight;
									if(offset_top > $(parentCell).height() - $(mainCell).height()) {
										offset_top = $(parentCell).height() - $(mainCell).height()
									}
								}
								$(mainCell).scrollTop(offset_top);
							}
						})
					})

					break;
				case 1:
					$(nextCell).hide();

					$.each(data, function() {
						shengjiang($(this)[0]);
					})
					break;
				case 2:
					$(nextCell).hide();
					$.each(data, function() {
						shengjiang($(this)[0]);
					})
					break;

			}

			function shengjiang(datas, types) {
				var overdue_list = document.createElement('li');
				overdue_list.className = "overdue-list";
				overdue_list.setAttribute('data-name', datas.name);
				overdue_list.setAttribute('data-phone', datas.phone);
				var str = '';

				str += '<div class="flex-1"><a href="/admin/mobile/mobile_cuishou/detail?cid='+datas.id+'"><img class="head-portrait" src="' + datas.head_portrait + '" /></a></div>';
				str += '<div class="flex-2"><a href="/admin/mobile/mobile_cuishou/detail?cid='+datas.id+'"><p class="overdue-name">' + datas.name + '</p><p class="overdue-phone"><i class="phone-icon"></i>' + datas.phone + '</p></a></div>';
				str += '<div class="flex-1"><p class="voerdue-money">' + datas.amount + '</p></div>';
				str += '<div class="flex-1"><p class="voerdue-state">逾期<em>' + Math.floor(parseInt(datas.overdue_days)) + '</em>天</p></div>';

				overdue_list.innerHTML = str;
				_this.append(overdue_list);

			}
			// 搜索框事件
			var search = document.getElementsByClassName(serchInput)[0];
			var pot = 0
			search.addEventListener("keyup", function() {
				if(this.value == '') {
					if(pot) {
						$("#overdue-group").html('');
						if(type == 0) {
							searchof(data);
						} else {
							$.each(data, function() {
								shengjiang($(this)[0]);
							})
						}
						pot = 0;
					}
				} else if(isNaN(parseInt(this.value))) {
					var value = this.value;
					var name = [];
					pot = 1;
					if(type == 0) {
						searchof(data);
					} else {
						$.each(data, function() {
							shengjiang($(this)[0]);
						})
					}
					$.each(data, function(index) {
						if($(this)[0].name.indexOf(value) >= 0) {
							name.push($(this)[0]);
						}

					});
					$("#overdue-group").html('');
					if(type == 0) {
						searchof(name);
					} else {
						$.each(name, function() {
							shengjiang($(this)[0]);
						})
					}
					
					name.splice(0, name.length)
				} else if(typeof parseInt(this.value) == 'number') {
					var value = this.value;
					var phone = [];
					pot = 1;
					$.each(data, function(index) {
						if($(this)[0].phone.indexOf(value) >= 0) {
							phone.push($(this)[0]);
						}

					});
					$("#overdue-group").html('');
					if(type == 0) {
						searchof(phone);
					} else {
						$.each(phone, function() {
							shengjiang($(this)[0]);
						})
					}
					
					phone.splice(0, phone.length)
				} else {
					alert('程序出问题')
				}

			});
		});
	}

})(jQuery);